package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.reggie.common.R;
import com.example.reggie.entity.AddressBook;
import com.example.reggie.service.impl.AddressBookServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookServiceImpl addressBookService;

    /*新增*/
    @PostMapping
    public R add(@RequestBody AddressBook addressBook, HttpServletRequest request){
        Long user = (Long)request.getSession().getAttribute("user");
        System.out.println(user);
        addressBook.setUserId(user);
        addressBookService.save(addressBook);
        return R.success("新增成功");
    }

    /*查询地址*/
    @GetMapping("/list")
    public R list(HttpServletRequest request,AddressBook addressBook){
        Long user = (Long) request.getSession().getAttribute("user");
        addressBook.setUserId(user);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(user!=null,AddressBook::getUserId,addressBook.getUserId());
        List<AddressBook> list = addressBookService.list(wrapper);

        return R.success(list);
    }

    /*设置为默认地址*/
    @PutMapping("/default")
    public R defaultD(@RequestBody AddressBook addressBook,HttpServletRequest request){

        System.out.println("ada" + addressBook);
        Long user = (Long) request.getSession().getAttribute("user");

        /*将所有的isDefault全部变成0*/
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        lambdaUpdateWrapper.eq(AddressBook::getUserId,user);
        addressBookService.update(lambdaUpdateWrapper);

        /**/
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("设置成功");
    }

    /*查询默认地址*/
    @GetMapping("/default")
    public R listDefault(HttpServletRequest request){

        Long user = (Long) request.getSession().getAttribute("user");

        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,user).eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(wrapper);

        log.info(one.getDetail());

        return R.success(one.getDetail());
    }
    /*根据id查询地址(回显)*/
    @GetMapping("/{id}")
    public R listId(@PathVariable Long id){

        AddressBook one = addressBookService.getById(id);

        return R.success(one);
    }

    /*删除*/
    @DeleteMapping
    public R deleteD(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /*修改*/
    @PutMapping
    public R up(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

}
